﻿using System;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Windows.Data;
using ParaEditor.Desktop.Interfaces;
using ParaEditor.Infrastructure;
using ParaEditor.SysMgr.Service.LogMgrService.Impl;
using Microsoft.Practices.Prism.Commands;

namespace ParaEditor.Desktop.ViewModel
{
    [Export(typeof(ILogMgrViewModel))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class LogMgrViewModel : ViewModelBase, ILogMgrViewModel
    {
        private ICollectionView logCollection;
        public ICollectionView LogCollection
        {
            get
            {
                return this.logCollection;
            }
            set
            {
                this.logCollection = value;
                this.RaisePropertyChangedEvent("LogCollection");
            }
        }

        //开始日期绑定值
        private DateTime _beginDate;
        public DateTime beginDate
        {
            get
            {
                return this._beginDate;
            }
            set
            {
                this._beginDate = value;
                this.RaisePropertyChangedEvent("beginDate");
            }
        }

        //结束日期绑定值     
        private DateTime _endDate;
        public DateTime endDate
        {
            get
            {
                return this._endDate;
            }
            set
            {
                this._endDate = value;
                this.RaisePropertyChangedEvent("endDate");
            }
        }

        private LogMgrServiceImpl _logMgrServiceImpl;

        public DelegateCommand QueryCommand { get; private set; }

        [ImportingConstructor]
        public LogMgrViewModel()
        {
            this.beginDate = DateTime.Now;
            this.endDate = DateTime.Now;
            //starttime = Convert.ToDateTime(beginDate).ToString("yyyyMMdd");
            //endtime = Convert.ToDateTime(endDate).ToString("yyyyMMdd");
            _logMgrServiceImpl = new LogMgrServiceImpl();
            this.QueryCommand = new DelegateCommand(this.QueryLogsByDate);

        }

        /// <summary>
        /// 根据时间段查询日志
        /// </summary>
        public void QueryLogsByDate()
        {
            string starttime = Convert.ToDateTime(beginDate).ToString("yyyyMMdd");
            string endtime = Convert.ToDateTime(endDate).ToString("yyyyMMdd");
            this.LogCollection = CollectionViewSource.GetDefaultView(_logMgrServiceImpl.GetLogsByDate(starttime, endtime));
        }

    }
}
